package streaming;

import simulation.ChunkStatusEnum;
import simulation.Message;


/**
 * Node requests packets when they are close to being out of its interest list - so that
 * other nodes won't request packets from it.
 */
public class Attacker4 extends Host {

	public Attacker4(int id) {
		super(id);
		isAttacker = true;
	}
	
	public void requestPacket() {

		periodicRequest.addEvent();
		auxCount++;
		
		if (neighborQueue.isEmpty()) {
			return;
		}

		int seqNo = -1;
		Neighbor neighbor = null;
		Host n = null;
		for (int i = 0; i < neighborQueue.size(); i++) {
			neighbor = (Neighbor)neighbors.get(new Integer(((Host)neighborQueue.get(0)).id));
			seqNo = neighbor.getOldNotification(interestWindow.getPacketStartSeqNo());
			n = (Host)neighborQueue.get(0);
			// check if there are any notified packets
			if (seqNo != -1)
				break;
			neighborQueue.remove(0);
			neighborQueue.add(n);
		}
		
		if (seqNo == -1) {
			return;
		}
		
		neighbor.addRequestedPacket(seqNo, simulator.getTime());

		// Check if host's outstanding limit of requests has been reached. - n is of class Host (unlike neighbor)
		if (neighbor.outstanding >= StreamingConfig.MCASTMAXOUTSTANDING) {
			neighborQueue.remove(n);
			outstandingNeighbors.add(n);
		}
		
		Neighbor[] ns = (Neighbor[])neighbors.values().toArray(new Neighbor[neighbors.size()]);
		// Remove sequence number from list of notified packets of other neighbors
		for (int i = 0; i < ns.length; i++)
			ns[i].removeNotifiedPacket(seqNo);
	
		new Message("request", this, n, seqNo, false);
		setPacketStatus(seqNo, ChunkStatusEnum.PENDING);
		
	}
	
}